<?php

class Default_Model_Newsgroup
{ 

    public $Name;
    public $Files;
    public $MinID;
    public $MaxID;
    public $Size;
    public $Count;
    public $Preview = NULL;
    public $Subscribed;
    public $DateModified;
    public $RelatedGroups;
    public $SavedSearches;
    function __construct($element=NULL)
    {
        if ($element) {
            $att=$element->attributes(); 
            $this->Name  = "{$att->name}"; 
            $this->MinID = floor($att['beginat']);
            $this->MaxID = floor($att['endat']);
            $this->Count = floor($att['count']);
        }
    }
 
    function GetSpan($newname) {
 
            $groupList = simplexml_load_file($newname);
            $node = $groupList->xpath('//list');
            return array(
                     'path' => $newname,
                     'min'  => $node[0]->min,
                     'max'  => $node[0]->max
                         ); 
    } 

    function GetRandom($Size=120) 
    { 
        $imager    = new Default_Model_Picture;
        $DATA_URI  = DATA_PATH . "/asset/{$this->Name}"; 
        $randomName = $this->GetRandomName() ; #echo  $randomName ; exit;
        if ($randomName) return $imager->Thumbnail($DATA_URI . '/' . $randomName, $Size);
        return NULL;
        
        $images    = scandir ($DATA_URI);
        $index     = rand (0, sizeof($images));
        $example   = $imager->ImageCreate($DATA_URI . '/' . $images[$index]);
        $limit     = 0;
        while (++$limit < 10 && !$example) {
            $index     = rand (0, sizeof($images));
            $example   = $imager->ImageCreate($DATA_URI . '/' . $images[$index]);
        } 
        if ($example) return $imager->Thumbnail($DATA_URI . '/' . $images[$index], $Size);
        return NULL;
    } 

    function GetRandomName() 
    { 
        $DATA_URI  = DATA_PATH . "/asset/{$this->Name}"; 
        if (!is_dir($DATA_URI)) return NULL;
        $images    = scandir ($DATA_URI);
        $index     = rand (0, sizeof($images));
        $imager    = new Default_Model_Picture;
        $example   = $imager->ImageCreate($DATA_URI . '/' . $images[$index]);
        $limit     = 0;
        while (++$limit < 10 && !$example) {
            $index     = rand (0, sizeof($images));
            $example   = $imager->ImageCreate($DATA_URI . '/' . $images[$index]);
        } 
        if ($example) return $images[$index];
        return NULL;
    } 

    function GetRandomArticle() 
    {
        $article   = $this->GetRandomInfo() ;
        $limit     = 0;
        while (++$limit < 3 && !$example)  
            $article = $this->GetRandomInfo() ; 
        return $article;
    } 

    function GetRandomInfo() 
    {
        $randomName = $this->GetRandomName() ;
        if (!$randomName) return NULL;
        $randomId = base64_decode ($randomName);

        $DATA_URI  = DATA_PATH . "/index/{$this->Name}.xml";
        $groupList = simplexml_load_file($DATA_URI); 
        $article   = $groupList->xpath("//article[id='{$randomId}']");  
        if (sizeof($article) > 0 && $article[0]->subject) return $article;
        return NULL;
    } 

    function Rejoin ($index) 
    {
         $DATA_URI = DATA_PATH . "/index/{$this->Name}.xml";
         $newname  = $this->Files[$index]['path'];   
         if (file_exists($newname))
         {
              rename ($DATA_URI, $DATA_URI.'.tmp');
              rename ($newname, $DATA_URI);
              rename ($DATA_URI.'.tmp', $newname); 
         }
         else {
             echo "ERROR: Could not rejoin {$this->Name} from {$newname}";
             var_dump ($this->Files);
             exit;
         }
    }

    function sync($Name=NULL, $short=NULL) 
    { 
        if ($Name) $this->Name = $Name;
        $DATA_URI  = DATA_PATH . "/index/{$this->Name}.xml";
        $FIND_URI  = DATA_PATH . "/search/{$this->Name}";
        $groupList = simplexml_load_file($DATA_URI);
        $node = $groupList->xpath('//list');
        $all  = $groupList->xpath('//list/article');
        $ref  = $groupList->xpath('//ref');
        $this->Count = sizeof($all);  
        $this->MinID = $node[0]->min;  
        $this->MaxID = $node[0]->max; 
        $this->DateModified  = fileatime($DATA_URI); 
        $this->RelatedGroups = array();
        $this->SavedSearches = array();
        if ($short) return;
        $this->Files = array($this->GetSpan($DATA_URI));
        $tmp = !is_dir($FIND_URI) ? array() : scandir ($FIND_URI);

        foreach ($tmp as $n) 
           if (!($n=='.' || $n=='..'))
               $this->SavedSearches[] = $n;

        foreach ($ref as $n) {
            $array = explode ('|', (string) $n);
	    for ($fn=0;$fn<sizeof($array);$fn++) {
                if ($array[$fn]!='Xref' && $array[$fn]!=$this->Name && !$this->RelatedGroups[$array[$fn]]) {
                    $this->RelatedGroups[$array[$fn]] = $array[$fn];
                }
            }
        }
	for ($fn=1;$fn<100;$fn++)
	{
            $newname = DATA_PATH . "/index/{$this->Name},{$fn}.xml";   
            if(file_exists($newname))
            {
                $this->Files[]   = $this->GetSpan($newname); 
            } else break;
        } 
    }
}

